import PixelCanvas from '../base/pixelCanvas'

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let scene = cc.director.getScene()
        this.canvas = scene.getChildByName('Canvas')
        this.graphics = this.node.getComponent(cc.Graphics)
        this.pixelCanvas = new PixelCanvas(-350, -150, 20, 20, 20)
        this.drawColor = cc.color(0, 162, 188, 255)
        this.lastGrid = cc.v2(0, 0)
    },

    start() {
        this.drawPixel()
        this.drawBorder()
        this.strokeGraphics()
    },

    drawBorder() {
        let pc = this.pixelCanvas
        let g = this.graphics
        let border = pc.getBorderData()
        g.strokeColor = new cc.Color(0, 162, 188, 255)
        g.rect(border.x, border.y, border.w, border.h)
    },

    drawPixel() {
        let pc = this.pixelCanvas
        let g = this.graphics
        let pixels = pc.getPixelData()
        for (let p of pixels) {
            g.fillColor = p.color
            g.rect(p.x, p.y, p.side, p.side)
            g.fill()
        }
    },

    // 因为会有很多地方需要stroke, 所以可以全部画完再stroke
    strokeGraphics() {
        let g = this.graphics
        g.stroke()
    },

    updateCanvas() {
        this.drawPixel()
        this.drawBorder()
        this.strokeGraphics()
    }

    // update (dt) {},
});
